﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraReports.UI;
using WindowsForms.AOnline.Model.BusinessObjects.Report;
using WindowsForms.AOnline.Presenter.Report;
using WindowsForms.AOnline.Report.ParameterReportForm;
using WindowsForms.AOnline.Report.Report;
using WindowsForms.AOnline.Report.ReportClass;
using WindowsForms.AOnline.Session;
using WindowsForms.AOnline.View.Report;

namespace WindowsForms.AOnline.Report.MainReport
{
    public partial class FrmReportList : XtraForm, IReportView, IReportGroupView
    {
        /// <summary>
        /// The _report list presenter
        /// </summary>
        private readonly ReportListPresenter _reportListPresenter;

        /// <summary>
        /// The _report group presenter
        /// </summary>
        private readonly ReportGroupPresenter _reportGroupPresenter;

        /// <summary>
        /// The _report list
        /// </summary>
        private List<ReportListModel> _reportList;
        public XtraReport xtraReport;
        public GlobalVariable CommonVariable { get; set; }
        
        public FrmReportList()
        {
            _reportGroupPresenter = new ReportGroupPresenter(this);
            _reportListPresenter = new ReportListPresenter(this);
            InitializeComponent();
        }

        /// <summary>
        /// Loads the data.
        /// </summary>
        public void LoadData()
        {
            try
            {
                _reportGroupPresenter.GetAllReportGroup();
                _reportList = _reportListPresenter.GetAllReportList();
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.ToString(), "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }

        }

        /// <summary>
        /// Loads the report group grid layout.
        /// </summary>
        private void LoadReportGroupGridLayout()
        {
            try
            {
                grdReportGroup.ForceInitialize();
                if (gridReportGroupView.RowCount <= 0)
                {
                    return;
                }
                gridReportGroupView.Columns["ReportGroupID"].Visible = false;
                gridReportGroupView.Columns["Description"].Visible = false;
                gridReportGroupView.Columns["IsVoucher"].Visible = false;
                gridReportGroupView.Columns["IsActive"].Visible = false;
                gridReportGroupView.Columns["IsActive"].Visible = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        /// <summary>
        /// Loads the report detail grid layout.
        /// </summary>
        private void LoadReportDetailGridLayout()
        {
            try
            {
                if (_reportList.Count <= 0) return;
                for (int i = 0; i < gridReportDetailView.Columns.Count; i++)
                {
                    if (gridReportDetailView.Columns[i].FieldName != "Name")
                    {
                        gridReportDetailView.Columns[i].Visible = false;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        public List<ReportListModel> ReportLists { set { grdReportDetail.DataSource = value.FindAll(item => item.ReportGroupId == (long)gridReportGroupView.GetFocusedRowCellValue("ReportGroupID")); } }
        public List<ReportGroupModel> ReportGroups { set { grdReportGroup.DataSource = value; } }
        private void FrmReportList_Load(object sender, EventArgs e)
        {
            LoadData();
            LoadReportGroupGridLayout();
            LoadReportDetailGridLayout();
        }
        private void gridReportGroupView_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            var reportGroupId = (long)gridReportGroupView.GetFocusedRowCellValue("ReportGroupID");
            if (_reportList == null) return;
            ReportLists = _reportList.FindAll(item => item.ReportGroupId == reportGroupId);
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            ViewReport();
        }

        /// <summary>
        /// Handles the DoubleClick event of the gridReportDetailView control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void gridReportDetailView_DoubleClick(object sender, EventArgs e)
        {
            ViewReport();
        }

        /// <summary>
        /// Views the report.
        /// </summary>
        private void ViewReport()
        {
            try
            {
                
                var reportID = gridReportDetailView.GetFocusedRowCellValue("Code").ToString();
                if (reportID == "DicAccount")
                {
                    
                    xtraReport = new Accounts();

                }
                if (reportID == "DicStock")
                {
                    xtraReport = new Stocks();
                }

                if (reportID == "DicAutoBusiness")
                {
                    xtraReport = new AutoBusinessReport();
                }

                if (reportID == "DicCustomer")
                {
                    xtraReport = new CustomerReport();
                }

                if (reportID == "DicDepartment")
                {
                    xtraReport = new DepartmentReport();
                }

                if (reportID == "DicFixedAssetCategory")
                {
                    xtraReport = new FixedAssetCategoryReport();
                }

                if (reportID == "DicFixedAsset")
                {
                    xtraReport = new FixedAssetReport();
                }

                if (reportID == "DicInventoryItem")
                {
                    xtraReport = new InventoryItemReport();
                }
                if (reportID == "DicVendor")
                {
                    xtraReport = new VendorReport();
                }

                if (reportID == "StockBacklog")
                {
                    xtraReport = new StockBacklogReport();

                    //using (var frmParam = new FrmStockBacklog())
                    //{
                    //    var fromDate = frmParam.FromDate;
                    //    var toDate = frmParam.ToDate;
                    //    var stockName = frmParam.StockName;

                    //    xtraReport.Parameters["FromDate"].Value = "01/01/2014";//fromDate.ToShortDateString();
                    //    xtraReport.Parameters["ToDate"].Value = "02/01/2014";//toDate.ToShortDateString();
                    //    xtraReport.Parameters["StockName"].Value = "Kho tiền";
                    //}
                    xtraReport.Parameters["CommuneReportPreparer"].Value = "Mai Chí Linh";//CommonVariable.CompanyChiefAccountantTitle;
                    xtraReport.Parameters["CompanyAccountant"].Value = "Hoàng Minh Tuấn";//CommonVariable.CompanyAccountant;
                    xtraReport.Parameters["CompanyDirector"].Value = "Đỗ Duy Thọ";//CommonVariable.CompanyDirector;
                }

                
                //       CommonVariable.FromDate = DateTime.Now;
                    //GlobalVariable.ToDate = 
                ReportTool.CommonVariable = CommonVariable;
                ReportTool.PrintPreview(this, _reportList, reportID, false, xtraReport);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}
